﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Data;
using ZFine.Domain.Entity;

namespace Logic.Repository.Project
{
    public class WorkPlanRespository : RepositoryBase<WorkPlanEntity>,IWorkPlanRespository
    {

        public int SubmitPlan(OperationGudaoCarEntity operationGudaoCarEntity, WorkPlanEntity WorkPlan)
        {
            if (WorkPlan.DiaoDuPerson != null)
            {
                dbcontext.Entry<UserEntity>(WorkPlan.DiaoDuPerson).State = EntityState.Unchanged;
                dbcontext.Entry(WorkPlan).Reference(f => f.DiaoDuPerson).CurrentValue =WorkPlan.DiaoDuPerson;
            }
            if (WorkPlan.DiaoChePersion != null)
            {
                dbcontext.Entry<UserEntity>(WorkPlan.DiaoChePersion).State = EntityState.Unchanged;
                dbcontext.Entry(WorkPlan).Reference(f => f.DiaoChePersion).CurrentValue = WorkPlan.DiaoChePersion;
            }
            dbcontext.Entry<WorkPlanEntity>(WorkPlan).State = EntityState.Added;
            dbcontext.SaveChanges();
            operationGudaoCarEntity.WorkPlan = WorkPlan;
            if (operationGudaoCarEntity.OperationPersion != null)
            {
                if (WorkPlan != null && WorkPlan.DiaoDuPerson != null)
                {
                    if (operationGudaoCarEntity.OperationPersion.F_Id == WorkPlan.DiaoDuPerson.F_Id)
                    {
                        operationGudaoCarEntity.OperationPersion = WorkPlan.DiaoDuPerson;
                    }
                }
                dbcontext.Entry<UserEntity>(operationGudaoCarEntity.OperationPersion).State = EntityState.Unchanged;
                dbcontext.Entry(operationGudaoCarEntity).Reference(f => f.OperationPersion).CurrentValue = operationGudaoCarEntity.OperationPersion;
            }
            if (operationGudaoCarEntity.WorkPlan != null)
            {
                dbcontext.Entry<WorkPlanEntity>(operationGudaoCarEntity.WorkPlan).State = EntityState.Unchanged;
                dbcontext.Entry(operationGudaoCarEntity).Reference(f => f.WorkPlan).CurrentValue = operationGudaoCarEntity.WorkPlan;
            }
           
            dbcontext.Entry<OperationGudaoCarEntity>(operationGudaoCarEntity).State = EntityState.Added;
            return dbcontext.SaveChanges();
        }
    }
}
